package ihm;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JTextArea;
import javax.swing.JButton;

import data.Authentification;
import data.Indice;

import data.ModeleListeIndices;
import javax.swing.SwingConstants;


public class FenetreVoirIndice extends JFrame {

	private JPanel contentPane;

	private ModeleListeIndices modele;
	private String Type_fichier;
	
	

	/**
	 * Create the frame.
	 * @return 
	 */
	public  FenetreVoirIndice(ModeleListeIndices mdoui,final Indice indice_infos) throws SQLException {
		setTitle("Murder Party - Voir un indice");		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
		
		
		
		modele = mdoui;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 337, 364);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblTitreDeLindice = new JLabel(indice_infos.getLabelIndice());
		lblTitreDeLindice.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitreDeLindice.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		lblTitreDeLindice.setBounds(84, 24, 176, 35);
		contentPane.add(lblTitreDeLindice);
		
		JTextArea textArea = new JTextArea(indice_infos.getDescription_Indice());
		textArea.setBounds(35, 70, 253, 166);
		contentPane.add(textArea);
		
		JButton btnNewButton = new JButton("Voir l'indice");
		btnNewButton.setBounds(45, 253, 223, 23);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				System.out.println(indice_infos.getCheminIndice());
				if ( !(indice_infos.getCheminIndice().equals("")) )
				{
				
				File fichier_indice = new File("data\\"+indice_infos.getCheminIndice() );
				System.out.println("data\\"+indice_infos.getCheminIndice() );
				boolean test =OuvrirFile(fichier_indice);
				if ( test == true )
				{
					System.out.println("test = true");
				}
				else
				{
					System.out.println("test = false");
				}
			}
				
			}
		});
		
		JButton btnQuitter = new JButton("Quitter");
		btnQuitter.setBounds(106, 303, 89, 23);
		contentPane.add(btnQuitter);
		btnQuitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				FermerFenetre();
			}
			});
	
		btnQuitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
			fermer_fenetre();
				
			}
		});
		
	
	
	}



	protected void fermer_fenetre() {
	this.dispose();
		
	}



	public boolean OuvrirFile(final File file) {
		  if (!Desktop.isDesktopSupported()) {
		    return false;
		  }

		  Desktop desktop = Desktop.getDesktop();
		  if (!desktop.isSupported(Desktop.Action.OPEN)) {
		    return false;
		  }

		  try {
			  
			  System.out.println("test");
		    desktop.open(file);
		  } catch (IOException e) {
		    // Log an error
		    return false;
		  }

		  return true;
		}

	private void FermerFenetre() {
		this.dispose();
			
		}
}
